package org.projectwithoutname.ui.pages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import model.Entity;
import model.Serie;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.part.EditorPart;
import org.projectwithoutname.commons.ICommandIds;
import org.projectwithoutname.ui.pages.input.EntityEditorInput;
import org.projectwithoutname.ui.util.ScreenToolkit;
import org.projectwithoutname.ui.util.Separator;

public class SinopsePage extends EditorPart {

	public static final String ID = ICommandIds.SINOPSE_PAGE; 
	private boolean dirty = false, check = false;
	private ScrolledForm form;
	private Text descriptionText;
	private FormToolkit formToolkit;
	private Text textName;
	private Label nameLabel;
	private Thread linkView;
	private ScreenToolkit layout;
	private Separator separator2;
	private Separator separator;
	private ArrayList<Control> widgets = new ArrayList<Control>();
	private Label generalInformationLabel;
	private Composite composite;
	
	

	@Override
	public void doSave(IProgressMonitor monitor) {
		Serie sinopse = (Serie) getSinopseEditorInput().getEntity();
		sinopse.setDescription(descriptionText.getText());
		sinopse.setName(textName.getText());
		System.out.println("Resource: "+sinopse.eResource());
		try {
			sinopse.eResource().save(Collections.EMPTY_MAP);
		} catch (IOException e) {
			e.printStackTrace();
		}
		setDirty(false);
		initTexts();	
	}
	/**
	 * <p>notify that the editor is dirty</p>
	 * @param isDirty : <b>true</b> if editor is dirty<br/>
	 *                 <b>false</b> if isn't
	 */
	private void setDirty(boolean isDirty) {
		dirty = isDirty;
		firePropertyChange(PROP_DIRTY);
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
		throws PartInitException{
			setSite(site);
			setInput(input);
			setPartName("Sinopse");	
		}

	@Override
	public boolean isDirty() {
		return dirty;
	}
	
	@Override
	public boolean isSaveAsAllowed() {
		return dirty;
	}

	@Override
	public void createPartControl(Composite parent){
		composite = parent;
		
		Color color = new Color(parent.getDisplay(), 122, 153, 194);
		parent.setBackground(color);
		
//		init screen section
		separator = new Separator(parent, SWT.BORDER_DASH);

		generalInformationLabel = new Label(parent, SWT.NONE);
		generalInformationLabel.setText("Overview");
		generalInformationLabel.setBackground(color);
		
		separator2 = new Separator(parent, SWT.BORDER_DASH);
		
		nameLabel = new Label(parent, SWT.NONE);
		nameLabel.setText("Name: ");
		nameLabel.setBackground(color);

		textName = new Text(parent, SWT.NONE);
		textName.setBackground(color);
		
		descriptionText = new Text(parent, SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		descriptionText.setBackground(color);
		
		descriptionText.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e) {
				setDirty(true);
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
		
		textName.addKeyListener(new KeyListener()
		{
			@Override
			public void keyPressed(KeyEvent e) {
				setDirty(true);
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
			}
		});
		
		refreshScreen(composite);
		
		composite.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent e) {
				refreshScreen(composite);
			}
			
			@Override
			public void controlMoved(ControlEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		composite.setLayout(null);
	}
	
	/**
	 * <p>organize the widgets on the screen when the control is resized.</p>
	 * @param composite : composite responsible to display the widgets
	 */
	private void refreshScreen(Composite composite){
		int width = composite.getSize().x - 10;
		separator.setBounds(0, 10, width + 10, 1);
		generalInformationLabel.setBounds(10, separator.getBounds().height + 15, 80, 17);
		separator2.setBounds(0, generalInformationLabel.getBounds().height + 20, width + 10, 1);
		
		nameLabel.setBounds(10, separator2.getBounds().height + 50, 42, 17);
		textName.setBounds(nameLabel.getBounds().width + 10, nameLabel.getBounds().y, 120, 17);
		
		descriptionText.setBounds(8, textName.getBounds().height + 60, width, 130);
		
		initTexts();
	}
	
	
	private void initTexts() {
		final Device device = this.getSite().getShell().getDisplay(); 
		Color white = new Color(device, 255, 255, 255);
		Serie sinopse = (Serie) getSinopseEditorInput().getEntity();
		
		if(sinopse.getName() == null){
			textName.setForeground(white);	
			textName.setText("Insira um nome."); 
		}
		else textName.setText(sinopse.getName());
		
		if(sinopse.getDescription() == null){
			descriptionText.setForeground(white);	
			descriptionText.setText("Write anything about it.");
		}
		else{
			descriptionText.setText(sinopse.getDescription());
		}
		
		final Color preto = new Color(device, 0, 0, 0);
		
		textName.addListener(SWT.MouseDown, new Listener(){
			public void handleEvent(Event event) {
				if (textName.getText().equals("Insira um nome.")){
					textName.setText("");
					textName.setForeground(preto);
				}	
			}
		});
		
		descriptionText.addListener(SWT.MouseDown, new Listener(){
			public void handleEvent(Event event) {
				if (descriptionText.getText().equals("Write anything about it.")){
					descriptionText.setText("");
					descriptionText.setForeground(preto);
				}
			}
		});
	}
	
	private EntityEditorInput getSinopseEditorInput() {
		return (EntityEditorInput) getEditorInput();
	}
	
	public void chageInput(Entity entity) {
		System.out.println("need u now");
		getSinopseEditorInput().setEntity(entity);
		initTexts();
		setDirty(true);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
